package cn.kailangtu.commontools;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.Network;
import android.net.NetworkCapabilities;
import android.net.NetworkInfo;
import android.net.NetworkRequest;
import android.os.Build;
import android.util.Log;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

public class ConnectUtils {

    private static boolean wifiConnected = false;
    private static boolean mobileConnected = false;
    private static boolean bluetoothConnected = false;
    private static boolean ethernetConnected = false;





    public static List<NetworkInfo> getNetworkInfo(Context context)
    {






        List<NetworkInfo> lsResult = new ArrayList<>();
        ConnectivityManager connectivityManager = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo[] networkArray =  connectivityManager.getAllNetworkInfo();
        boolean currWifiConn = false;
        boolean currMobileConn= false;
        boolean currBluetoothConn = false;
        boolean currEthernetConn = false;
        for(NetworkInfo network : networkArray){
            Log.d("ConnectUtils-->NetworkInfo", "NAME:"+network.getTypeName()+" || "+"CONN:"+network.isConnected()
                    + " ||| ACTIVITY:"+network.isAvailable() + " ||| "+"FAILOVER:"+network.isFailover()
            );

            if(network.isConnected())
            {
                if(network.getTypeName().indexOf("WIFI")> -1)
                {
                    currWifiConn = true;
                }
                else if(network.getTypeName().indexOf("MOBILE")> -1){
                    currMobileConn = true;
                }
                else if(network.getTypeName().indexOf("BLUETOOTH") > -1)
                {
                    //currBluetoothConn = true;
                }
                else if(network.getTypeName().indexOf("ETHERNET") > -1){
                    currEthernetConn = true;
                }
                lsResult.add(network);
            }
        }

        ethernetConnected =  isEthernetConnected(context);

        wifiConnected = currWifiConn;
       // ethernetConnected = currEthernetConn;
        mobileConnected = currMobileConn;
        bluetoothConnected = currBluetoothConn;
        return lsResult;
    }



    public static boolean isEthernetConnected(Context context) {
        ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            Network[] networks = connectivityManager.getAllNetworks();
            for (Network network : networks) {
                NetworkCapabilities capabilities = connectivityManager.getNetworkCapabilities(network);
                if (capabilities != null && capabilities.hasTransport(NetworkCapabilities.TRANSPORT_ETHERNET)) {
                    Log.d("isEthernetConnected","1");
                    return true;
                }
            }
        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            NetworkRequest.Builder builder = new NetworkRequest.Builder();
            builder.addTransportType(NetworkCapabilities.TRANSPORT_ETHERNET);
            NetworkRequest request = builder.build();

            connectivityManager.requestNetwork(request, new ConnectivityManager.NetworkCallback() {
                @Override
                public void onAvailable(Network network) {
                    // 以太网已连接
                    Log.d("isEthernetConnected","2");

                }

                @Override
                public void onLost(Network network) {
                    // 以太网连接已丢失
                    Log.d("isEthernetConnected","3");
                }
            });
        }

        return false;
    }


    public static boolean isWifiConnected() {
        return wifiConnected;
    }

    public static void setWifiConnected(boolean wifiConnected) {
        ConnectUtils.wifiConnected = wifiConnected;
    }

    public static boolean isMobileConnected() {
        return mobileConnected;
    }

    public static void setMobileConnected(boolean mobileConnected) {
        ConnectUtils.mobileConnected = mobileConnected;
    }

    public static boolean isBluetoothConnected() {
        return bluetoothConnected;
    }

    public static void setBluetoothConnected(boolean bluetoothConnected) {
        ConnectUtils.bluetoothConnected = bluetoothConnected;
    }

    public static boolean isEthernetConnected() {
        return ethernetConnected;
    }

    public static void setEthernetConnected(boolean ethernetConnected) {
        ConnectUtils.ethernetConnected = ethernetConnected;
    }
}
